using PKGames.Localization;
using System;
using UnityEngine;
using UnityEngine.UI;

public class LocalizeTextField : MonoBehaviour
{
	[Serializable]
	public class LocalizationField
	{
		[SerializeField]
		private Text textField;

		[SerializeField]
		private string localizationKey;

		public Text TextField => textField;

		public string LocalizationKey
		{
			get
			{
				return localizationKey;
			}
			set
			{
				localizationKey = value;
			}
		}
	}

	[SerializeField]
	private LocalizationField[] _localizationFields;

	public LocalizationField[] LocalizationFields
	{
		get
		{
			return _localizationFields;
		}
		set
		{
			_localizationFields = value;
		}
	}

	private void Start()
	{
		//UpdateLocalizedText();
	}

	public void UpdateLocalizedText()
	{
		LocalizationField[] localizationFields = _localizationFields;
		foreach (LocalizationField localizationField in localizationFields)
		{
			string language = MonoSingleton<LocationProvider>.Instance.GetLanguage();
			try
			{
				language = MonoSingleton<LocationProvider>.Instance.GetLanguageWithFallback();
			}
			catch (SupportedLanguageResolverException ex)
			{
				UnityEngine.Debug.LogWarning("[LocalizedTextField] error getting langauge with fallback: " + ex.Message);
			}
			LocalizedText localizedText = new LocalizedText(localizationField.LocalizationKey, language);
			localizationField.TextField.text = localizedText.Value;
		}
	}
}
